#!/usr/bin/python
 # -*- coding: utf8 -*-
import os,sys,codecs

def createPage(files):
	res=u"""<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>
  <head>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
    <title>Slides</title>
  </head>
	<script language="JavaScript">
	<!--
		var i=1;
		function next(){
			document.getElementById(i+"").style.display="none";
			i++;
			if(document.getElementById(i+"")==null)
				i--;
			document.getElementById(i+"").style.display="inline";
			
		}	
		function prev(){
			document.getElementById(i+"").style.display="none";
			i--;
			if(document.getElementById(i+"")==null)
				i++;
			document.getElementById(i+"").style.display="inline";
		}	
		function about(){
			old=document.getElementById("about").style.display;
			document.getElementById("about").style.display=(old=="inline")? "none" : "inline";
		}	
		// kompatibilität mit browsern ohne JS
		function ini(){
			document.getElementById("1").style.display="inline";
			
		}
	//-->
	</script>

  <body onload="ini()">

	<div style="float:left;">
    <a href="#" onclick="prev()">prev</a>
	</div>
	

	<div>"""
	i=1
	
	files.sort();
	
	for f in files:
		res=res+u"""<object style="display:none" id=\""""+str(i)+"""\" data=\""""+str(f)+"""\" type="image/svg+xml" width="100%" height="90%">
		<param name="src" value=\""""+str(f)+"""\">your browser cannot show svg graphics
	</object>"""	
		i+=1
	res=res+u"""</div>
	<div style="float:right;top:0%">
	<a href="#" onclick="next()">next</a>
	</div>
	<div style="font-size:0.7em">&copy;stg7 2010 - <a href="#about" onclick="about()">about</a></div>
	<div id="about" style="display:none; position:absolute; top:10%;left:30%;width:40%;height:50%;background-color:#dddddd">
		<a href="#about" onclick="about()"><small>[x]</small></a> <br />
		<h1>About</h1>
		<p>
		 Script zum Erstellen von Online Präsentationen mit Hilfe von SVG Grafiken.<br>
		 Beamer Latex Präsentation -> PDF -> SVG -> mit Script zu Präsentation
		 
		 </p>
		<h3>Author</h3>
		<p>
		 Steve Göring
		</p>	
	</div>
  </body>
</html>"""
	return res

def main(argv):                         
  
	if len(argv)==2:
		# System aufruf -> für pdf2svg
		try:
			os.system("mkdir -p "+argv[1]) # verzeichnis erzeugen für die bilder
		except Error:
			print "could not create directory " 		
		os.system("pdf2svg "+argv[0]+" "+argv[1]+"/slides%04d.svg all")
		
		
		files=[argv[1]+"/"+x for x in os.listdir(argv[1]+"/") ]
		
		out = file( "slides.html", "w" )
		out.write( codecs.BOM_UTF8 )
		
		out.write( createPage(files).encode( 'utf-8' ) )
		out.close()

		
		
		#print createPage([1,2,3,4])
		
	else:
		print "usage: convert pdffile picdir \n you must install pdf2svg"
		sys.exit(2)			
	

if __name__ == "__main__":
    main(sys.argv[1:])



